﻿using System;
using System.Media;
using System.Timers;

namespace maranov.TeaTimer
{
    /// <summary>
    /// Jednoduché zvukové upozornění.
    /// </summary>
    public class SoundAlert : ISoundAlert 
    {
        /// <summary>
        /// Přehrávač uživatelského zvuku.
        /// </summary>
        private SoundPlayer CustomAlert = new SoundPlayer();
        /// <summary>
        /// Časovač omezené délky přehrávání.
        /// </summary>
        private Timer PlaybackLimiter = new Timer();

        public int MaximumPlaybackTime
        {
            get;
            set;
        }

        public bool PlaybackTimeLimited
        {
            get { return MaximumPlaybackTime > 0; }
        }

        public SoundAlert(int maximumPlaybackTime)
        {
            MaximumPlaybackTime = maximumPlaybackTime;
        }

        public AlertSound AlertSoundType
        {
            get;
            set;
        }

        public string CustomSoundFilePath
        {
            get;
            set;
        }

        public void Play()
        {
            switch (AlertSoundType)
            {
                case AlertSound.Custom:
                    CustomAlert.SoundLocation = CustomSoundFilePath;
                    try
                    {
                        CustomAlert.Play();
                    }
                    catch
                    {
                        SystemSounds.Exclamation.Play();
                    }

                    if (PlaybackTimeLimited)
                    {
                        PlaybackLimiter.Interval = MaximumPlaybackTime * 1000;
                        PlaybackLimiter.Elapsed += new ElapsedEventHandler(
                            delegate(Object sender, ElapsedEventArgs e)
                            {
                                CustomAlert.Stop();
                            });
                        PlaybackLimiter.Start();
                    }
                    break;

                case AlertSound.Beep:
                    SystemSounds.Beep.Play();
                    break;

                case AlertSound.Exclamation:
                    SystemSounds.Exclamation.Play();
                    break;

                case AlertSound.None:
                default:
                    // Bez zvuku
                    break;
            }
        }

        public void StopPlayback()
        {
            if (AlertSoundType == AlertSound.Custom)
            {
                CustomAlert.Stop();
                if (PlaybackTimeLimited)
                {
                    PlaybackLimiter.Stop();
                }
            }
        }

        /// <summary>
        /// Zjistí, zda je typ souboru podporován.
        /// </summary>
        /// <param name="extension">Přípona (vč. tečky).</param>
        /// <returns>True, pokud je podporována.</returns>
        static public bool IsSupportedFileExtension(string extension)
        {
            return extension == ".wav" || extension == ".wave";
        }
    }
}
